package br.webchild.client;

import com.google.gwt.core.client.GWT;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.smartgwt.client.types.Alignment;
import com.smartgwt.client.types.DateDisplayFormat;
import com.smartgwt.client.types.TitleOrientation;
import com.smartgwt.client.util.SC;
import com.smartgwt.client.widgets.Window;
import com.smartgwt.client.widgets.events.ClickEvent;
import com.smartgwt.client.widgets.events.ClickHandler;
import com.smartgwt.client.widgets.form.DynamicForm;
import com.smartgwt.client.widgets.form.fields.DateItem;
import com.smartgwt.client.widgets.form.fields.DateTimeItem;
import com.smartgwt.client.widgets.form.fields.HeaderItem;
import com.smartgwt.client.widgets.form.fields.PickerIcon;
import com.smartgwt.client.widgets.form.fields.RowSpacerItem;
import com.smartgwt.client.widgets.form.fields.SelectItem;
import com.smartgwt.client.widgets.form.fields.TextItem;
import com.smartgwt.client.widgets.form.fields.events.FormItemClickHandler;
import com.smartgwt.client.widgets.form.fields.events.FormItemIconClickEvent;
import com.smartgwt.client.widgets.grid.ListGrid;
import com.smartgwt.client.widgets.layout.VLayout;
import com.smartgwt.client.widgets.tab.Tab;
import com.smartgwt.client.widgets.tab.TabSet;
import com.smartgwt.client.widgets.toolbar.ToolStrip;
import com.smartgwt.client.widgets.toolbar.ToolStripButton;

public class Paciente extends Window {

	private final TextItem Codigo;
	private final TextItem Nome;
	private final TextItem Endereco;
	private final TextItem Numero;
	private final TextItem Bairro;
	private final TextItem Complemento;
	private final TextItem Cidade;
	private final SelectItem Estado;
	private final TextItem DataNasc;
	private final TextItem DataCad;
	private final SelectItem Sexo;
	private final TextItem NomeResp;
	private final TextItem RgResp;
	private final TextItem CpfResp;
	private final TextItem EnderecoResp;
	private final TextItem BairroResp;
	private final TextItem NumResp;
	private final TextItem ComplementoResp;
	private final TextItem CidadeResp;
	private final SelectItem EstadoResp;
	private final TextItem FoneResp;
	private final TextItem FoneCelResp;
	private final TextItem FoneComerResp;
	private final ToolStrip menuCadastro;
	private final ToolStripButton btn_salvar;
	private final ToolStripButton btn_limpar;
	private final HeaderItem Responsavel;
	private HeaderItem Paciente;
	
	private final PacienteServicoAsync servico = GWT.create(PacienteServico.class);
//	ATUALIZADO DIA: 07/08 01:20HRS
	final AsyncCallback<Boolean> resposta = new AsyncCallback<Boolean>(){

		@Override
		public void onFailure(Throwable caught) {
			SC.say("erro no servidor...");
		}

		@Override
		public void onSuccess(Boolean result) {
			SC.say("Gravado com Sucesso!!!");
		}
	};

	public Paciente() {
		this.setTitle("Paciente");
		setSize("720px", "520px");
		this.setShowMinimizeButton(false);
		this.setIsModal(true);
		this.centerInPage();
		this.setHeaderIcon("/imagens/paciente.png");
		
		final DateItem dateItem = new DateItem();
		dateItem.setUseTextField(true);
		dateItem.setDateFormatter(DateDisplayFormat.TOEUROPEANSHORTDATE);
		java.sql.Date data = new java.sql.Date(System.currentTimeMillis()); // DATA
																			// ATUAL

		TabSet tabelaAbas = new TabSet();
		tabelaAbas.setHeight("100%");

		// **********ABA CADASTRO*********
		Tab aba_cadastro = new Tab("Cadastro");

		VLayout vl_cadastro = new VLayout();

		DynamicForm form_cadastro = new DynamicForm();
		form_cadastro.setNumCols(4);

		Codigo = new TextItem();
		Codigo.setTitle("C&oacute;digo");
//		tI_Codigo.setDisabled(true);

		Nome = new TextItem();
		Nome.setTitle("Nome");

		Endereco = new TextItem();
		Endereco.setTitle("Endere&ccedil;o");

		Numero = new TextItem();
		Numero.setTitle("N&uacute;mero");

		Bairro = new TextItem();
		Bairro.setTitle("Bairro");

		Complemento = new TextItem();
		Complemento.setTitle("Complemento");

		Cidade = new TextItem();
		Cidade.setTitle("Cidade");

		Estado = new SelectItem();
		Estado.setTitle("Estado");
		Estado.setValueMap("AC", "AL", "AM", "AP", "BA", "CE", "DF", "ES","GO", "MA", "MG", "MS", "MT", "PA", "PB", "PE", "PI", "PR","RJ", "RN", "RO", "RR", "RS", "SC", "SE", "SP", "TO");

		DataNasc = new TextItem();
		DataNasc.setTitle("Data Nascimento");
		DataNasc.setEditorType(dateItem);
		DataNasc.setTextAlign(Alignment.LEFT);

		DataCad = new TextItem();
		// tiDataCad.setVisible(false);
		DataCad.setTitle("Data Cadastro");
//		tiDataCad.setDisabled(true);
		DataCad.setDateFormatter(DateDisplayFormat.TOEUROPEANSHORTDATE); // FORMATA
																			// DATA
		DataCad.setValue(data);

		Sexo = new SelectItem();
		Sexo.setTitle("Sexo");
		Sexo.setValueMap("MASCULINO", "FEMININO");

		NomeResp = new TextItem();
		NomeResp.setTitle("Nome Respons&aacute;vel");

		RgResp = new TextItem();
		RgResp.setTitle("RG do Respons&aacute;vel");
		RgResp.setMask("#.###.###-#");

		CpfResp = new TextItem();
		CpfResp.setTitle("CPF do Respons&aacute;vel");
		CpfResp.setMask("###.###.###-##");

		EnderecoResp = new TextItem();
		EnderecoResp.setTitle("Endereco do Resp");

		BairroResp = new TextItem();
		BairroResp.setTitle("Bairro");

		NumResp = new TextItem();
		NumResp.setTitle("Numero");

		ComplementoResp = new TextItem();
		ComplementoResp.setTitle("Complemento");

		CidadeResp = new TextItem();
		CidadeResp.setTitle("Cidade");

		EstadoResp= new SelectItem();
		EstadoResp.setTitle("Estado");
		EstadoResp.setValueMap("AC", "AL", "AM", "AP", "BA", "CE", "DF", "ES","GO", "MA", "MG", "MS", "MT", "PA", "PB", "PE", "PI", "PR","RJ", "RN", "RO", "RR", "RS", "SC", "SE", "SP", "TO");

		FoneResp = new TextItem();
		FoneResp.setTitle("Telefone");
		FoneResp.setMask("(##)####-####");
		
		FoneCelResp = new TextItem();
		FoneCelResp.setTitle("Telefone Celular");
		FoneCelResp.setMask("(##)####-####");

		FoneComerResp = new TextItem();
		FoneComerResp.setTitle("Telefone Comercial");
		FoneComerResp.setMask("(##)####-####");
		
		Responsavel = new HeaderItem();
		Responsavel.setDefaultValue("Respons&aacute;vel"); 
		
		Paciente = new HeaderItem();
		Paciente.setDefaultValue("Paciente"); 
		
		
		RowSpacerItem espaco = new RowSpacerItem();

		form_cadastro.setItems(Codigo, Nome, Endereco, Numero,
				Bairro, Complemento, Cidade, Estado, DataNasc,
				Sexo, espaco, NomeResp, RgResp, CpfResp,
				EnderecoResp, NumResp, BairroResp, ComplementoResp,
				CidadeResp, EstadoResp, FoneResp, FoneCelResp,
				FoneComerResp, DataCad);
		form_cadastro.setTitleOrientation(TitleOrientation.TOP);
		
//		, hI_responsavel hI_paciente,

		menuCadastro = new ToolStrip();
		menuCadastro.setWidth("100%");

		btn_salvar = new ToolStripButton("Salvar");
		btn_salvar.setIcon("/imagens/save_add.png");
		menuCadastro.addButton(btn_salvar);

		btn_limpar = new ToolStripButton("Limpar");
		btn_limpar.setIcon("/imagens/clear.png");
		menuCadastro.addButton(btn_limpar);

		vl_cadastro.addMember(menuCadastro);
		vl_cadastro.addMember(form_cadastro);
		aba_cadastro.setPane(vl_cadastro);
		tabelaAbas.addTab(aba_cadastro);

		btn_salvar.addClickHandler(new ClickHandler() {
			
			@Override
			public void onClick(ClickEvent event) {
				servico.novo(Nome.getValueAsString(), 
							Endereco.getValueAsString(), 
							Numero.getValueAsString(),
							Bairro.getValueAsString(),
							Complemento.getValueAsString(),
							Cidade.getValueAsString(),
							Estado.getValueAsString(),
							DataNasc.getValueAsString(),
							Sexo.getValueAsString(),
							NomeResp.getValueAsString(),
							RgResp.getValueAsString(),
							CpfResp.getValueAsString(),
							EnderecoResp.getValueAsString(),
							NumResp.getValueAsString(),
							BairroResp.getValueAsString(),
							ComplementoResp.getValueAsString(),
							CidadeResp.getValueAsString(),
							EstadoResp.getValueAsString(),
							FoneResp.getValueAsString(),
							FoneCelResp.getValueAsString(),
							FoneComerResp.getValueAsString(),
							DataCad.getValueAsString(),
							resposta
							);
				
			}
		});
		
		
		
		
		// ***************ABA CONSULTA***************
		Tab aba_consulta = new Tab("Consulta");

		VLayout vl_consulta = new VLayout();

		DynamicForm form_consulta = new DynamicForm();
		form_consulta.setNumCols(6);

		
		// ICONE NO CAMPO
		PickerIcon pi_busca = new PickerIcon(PickerIcon.SEARCH,
				new FormItemClickHandler() {
					public void onFormItemClick(FormItemIconClickEvent event) {
						SC.say("Busca por nome");
					}
				});

		
		TextItem ti_codigo = new TextItem();
		ti_codigo.setTitle("C&oacute;digo");

		TextItem ti_nome = new TextItem();
		ti_nome.setTitle("Nome");
		ti_nome.setIcons(pi_busca);

		DateTimeItem dti_data = new DateTimeItem();
		dti_data.setDateFormatter(DateDisplayFormat.TOEUROPEANSHORTDATE);
		dti_data.setTextAlign(Alignment.LEFT);
		dti_data.setTitle("Data");

		form_consulta.setItems(ti_codigo, ti_nome, dti_data);
		vl_consulta.addMember(form_consulta);

		ToolStrip menu_consulta = new ToolStrip();
		menu_consulta.setWidth("100%");

		ToolStripButton btn_pesquisar = new ToolStripButton("Pesquisar");
		btn_pesquisar.setIcon("/imagens/pesquisar.png");
		menu_consulta.addButton(btn_pesquisar);
		vl_consulta.addMember(menu_consulta);

		ListGrid listGrid = new ListGrid();
		vl_consulta.addMember(listGrid);
		aba_consulta.setPane(vl_consulta);
		tabelaAbas.addTab(aba_consulta);

		// ADICIONA O CONJUNTO DE ABAS
		addItem(tabelaAbas);

	} // FINAL DO CONSTRUTOR

}